﻿using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.IO.Ports;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Xml;
using PowerMonitor.BaseCtrl;
using PowerMonitor.Common;
using PowerMonitor.ModbusDev;
using PowerMonitor.Properties;

namespace PowerMonitor
{
    public partial class ModbusDevPara : Form//baseDockForm
    {
        private TreeNode SelNode = null;
        InnerModbusDevice CurDev = null;
        private string strRootName = "InnerModbus";
        private string strRootText = "设备列表";
        private Array _byteOrders = Enum.GetValues(typeof (eByteSqueue));

        public ModbusDevPara()
        {
            InitializeComponent();
        }
        private void ModbusDevPara_Load(object sender, EventArgs e)
        {
            strRootName = tvDevices.Nodes[0].Name;
            toolTip1.ToolTipTitle = Resources.msgTitleTip;
            cbbByteSqueue.Items.Clear();
            if (Thread.CurrentThread.CurrentUICulture.Name == "en")
            {
                cbbByteSqueue.Items.AddRange(new object[] { "HiByteLoByte", "LoByteHiByte", "HiWordLoWord", "LoWordHiWord", "3-4-1-2" });
            }
            else
            {
                cbbByteSqueue.Items.AddRange(new object[] { "高位在前", "低位在前", "高字在前", "低字在前", "三四一二" });
            }
            RefreshChlDevTree();
            if (tvDevices.Nodes[0].Nodes.Count > 0 && (tvDevices.Nodes[0].Nodes[0].Nodes.Count>0))
            {
                tvDevices.SelectedNode = tvDevices.Nodes[0].Nodes[0].Nodes[0];
            }
            
           
        }
        private bool RefreshChlDevTree()
        {
            //dbOper db = new dbOper();
            TreeNode tn = null;
            int ChlCnt = 0;
            InnerModbusChannel tempChl = null;
            dgvChlPara.Rows.Clear();
            if (AppMain.ChlList.Count <= 0)
            {
                tvDevices.Nodes.Clear(); // 清空所有
                TreeNode RootNode = new TreeNode(strRootText);
                RootNode.Name = strRootName;
                RootNode.ImageIndex = 1;
                RootNode.SelectedImageIndex = 0;
                tvDevices.Nodes.Add(RootNode);
                dgvChlPara.Rows.Clear();
                return false;
            }
            dgvChlPara.Rows.Add(AppMain.ChlList.Count);
            if (AppMain.ChlList.Count < tvDevices.Nodes[0].Nodes.Count)
            {
                int NodeCnt = tvDevices.Nodes[0].Nodes.Count;
                for (int s = NodeCnt; s > AppMain.ChlList.Count; s--)
                {
                    tvDevices.Nodes[0].Nodes.RemoveAt(s - 1);
                }
            }
            for (int i = 0; i < AppMain.ChlList.Count; i ++)
            {
                tempChl = AppMain.ChlList[i] as InnerModbusChannel;
                if (tempChl == null)
                {
                    continue;
                }
                if (tvDevices.Nodes[0].Nodes.Count > i)
                {
                    tn = tvDevices.Nodes[0].Nodes[i];
                    ChlCnt++;
                    tn.Name = tempChl.ComChlPara.ChannelId.ToString();// 通道名称 以及 装置名称 均使用ID(ChlId DevId)作为标识
                    tn.Text = tempChl.ComChlPara.ChannelName;         // 显示名称可自定义
                    if (tempChl.m_DeviceList.Count< tn.Nodes.Count)
                    {
                        int NodeCnt = tn.Nodes.Count;
                        for (int s = NodeCnt;s>tempChl.m_DeviceList.Count;s--)
                        {
                            tn.Nodes.RemoveAt(s - 1);
                        }
                    }
                    for (int j = 0; j < tempChl.m_DeviceList.Count; j++)
                    {
                        if (tn.Nodes.Count>j)
                        {
                            tn.Nodes[j].Name = tempChl.m_DeviceList[j].DeviceId.ToString();
                            tn.Nodes[j].Text = tempChl.m_DeviceList[j].Name;
                        } 
                        else
                        {
                            tn.Nodes.Add(tempChl.m_DeviceList[j].DeviceId.ToString(), tempChl.m_DeviceList[j].Name);
                        }
                    }
                }
                else
                {
                    tn = new TreeNode();
                    ChlCnt++;
                    tn.Name = tempChl.ComChlPara.ChannelId.ToString();// 通道名称 以及 装置名称 均使用ID(ChlId DevId)作为标识
                    tn.Text = tempChl.ComChlPara.ChannelName;         // 显示名称可自定义
                    if (tempChl.m_DeviceList.Count < tn.Nodes.Count)
                    {
                        int NodeCnt = tn.Nodes.Count;
                        for (int s = NodeCnt; s > tempChl.m_DeviceList.Count; s--)
                        {
                            tn.Nodes.RemoveAt(s - 1);
                        }
                    }
                    for (int j = 0; j < tempChl.m_DeviceList.Count; j++)
                    {
                        if (tn.Nodes.Count > j)
                        {
                            tn.Nodes[j].Name = tempChl.m_DeviceList[j].DeviceId.ToString();
                            tn.Nodes[j].Text = tempChl.m_DeviceList[j].Name;
                        }
                        else
                        {
                            tn.Nodes.Add(tempChl.m_DeviceList[j].DeviceId.ToString(), tempChl.m_DeviceList[j].Name);
                        }
                    }
                    tvDevices.Nodes[0].Nodes.Add(tn);
                }

                dgvChlPara.Rows[i].Cells[0].Value = tempChl.ComChlPara.ChannelName;
                if (((DataGridViewComboBoxCell)dgvChlPara.Rows[i].Cells[1]).Items.Contains(tempChl.ComChlPara.ComPort.PortName))
                {
                    ((DataGridViewComboBoxCell)dgvChlPara.Rows[i].Cells[1]).Value = tempChl.ComChlPara.ComPort.PortName;
                }
                if (((DataGridViewComboBoxCell)dgvChlPara.Rows[i].Cells[2]).Items.Contains(tempChl.ComChlPara.ComPort.BaudRate.ToString()))
                {
                    ((DataGridViewComboBoxCell)dgvChlPara.Rows[i].Cells[2]).Value = tempChl.ComChlPara.ComPort.BaudRate.ToString();
                }
                dgvChlPara.Rows[i].Cells[3].Value = tempChl.PollingTime;
                dgvChlPara.Rows[i].Cells[4].Value = tempChl.ComChlPara.iTimeOut;
                dgvChlPara.Rows[i].Cells[5].Value = tempChl.ComChlPara.ComPort.Parity.ToString();
                dgvChlPara.Rows[i].Cells[6].Value = tempChl.ComChlPara.ComPort.DataBits.ToString();
                switch (tempChl.ComChlPara.ComPort.StopBits.ToString())
                {
                    case "One":
                        dgvChlPara.Rows[i].Cells[7].Value = "1";
                        break;
                    case "OnePointFive":
                        dgvChlPara.Rows[i].Cells[7].Value = "1.5";
                        break;
                    case "Two":
                        dgvChlPara.Rows[i].Cells[7].Value = "2";
                        break;
                    default:
                        dgvChlPara.Rows[i].Cells[7].Value = "1";
                        break;
                }
            }
            tvDevices.ExpandAll();
            if (ChlCnt==0)
            {
                return false;
            }
            else
            {
                return true;
            }
            
        }
        private bool InitDevInfo(InnerModbusDevice pDevice)
        {
            if (pDevice == null)
            {
                txtDevName.Text = "";
                numAddr.Value = 0;
                ckbIsWorking.Checked = false;
                cbbByteSqueue.SelectedIndex = 0;// eByteSqueue.高字在前.ToString();
                numSendCnt.Value = 0;
                dgvDevInfo.Rows.Clear();
                return false;
            }
            dgvDevInfo.Rows.Clear();
            if (pDevice.GetDataList() != null && pDevice.GetDataItemCnt() > 0)
            {
                dgvDevInfo.Rows.Add(pDevice.GetDataItemCnt());
            }
            try
            {
                txtDevName.Text = pDevice.Name;
                numAddr.Value = pDevice.bAddr;
                ckbIsWorking.CheckState = (pDevice.isWorking== true ? CheckState.Checked: CheckState.Unchecked);
                cbbByteSqueue.SelectedIndex = (Int32) pDevice.byteSqueue;
                numSendCnt.Value = pDevice.MaxSendCnt - 1;
                numBreakCnt.Value = pDevice.BreakOffCnt;
                if (pDevice.GetDataList() == null)
                {
                    return true;
                }
                for (int i = 0; i < pDevice.GetDataItemCnt(); i++)
                {
                    dgvDevInfo.Rows[i].Cells[0].Value = pDevice.GetDataItem(i).Name;
                    if (((DataGridViewComboBoxCell)dgvDevInfo.Rows[i].Cells[1]).Items.Contains("0x" + pDevice.GetDataItem(i).bFunCode.ToString("X2")))
                    {
                        ((DataGridViewComboBoxCell)dgvDevInfo.Rows[i].Cells[1]).Value = "0x" + pDevice.GetDataItem(i).bFunCode.ToString("X2");
                    }
                    dgvDevInfo.Rows[i].Cells[2].Value = pDevice.GetDataItem(i).usRegAddr.ToString("X4");
                    if (((DataGridViewComboBoxCell)dgvDevInfo.Rows[i].Cells[3]).Items.Contains(pDevice.GetDataItem(i).dataType.ToString()))
                    {
                        ((DataGridViewComboBoxCell)dgvDevInfo.Rows[i].Cells[3]).Value = pDevice.GetDataItem(i).dataType.ToString();
                    }
                    dgvDevInfo.Rows[i].Cells[4].Value = pDevice.GetDataItem(i).dmult;
                    dgvDevInfo.Rows[i].Cells[5].Value = pDevice.GetDataItem(i).IsSummation;
                    dgvDevInfo.Rows[i].Cells[6].Value = pDevice.GetDataItem(i).IsSave;
                    dgvDevInfo.Rows[i].Cells[7].Value = pDevice.GetDataItem(i).SaveCycle;
                    dgvDevInfo.Rows[i].Tag = pDevice.GetDataItem(i).Expression;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            return true;
        }
        private void tvDevices_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                ApplyCurDevInfo();
                if (tvDevices.SelectedNode.Name == strRootName || tvDevices.SelectedNode.Parent.Name == strRootName) // 固定名称
                {
                    SelNode = null;
                    CurDev = null;
                    InitDevInfo(CurDev);  // 显示参数初始化
                    tabControl1.SelectedIndex = 1; // 切换TabControl显示
                    return;
                }

                tabControl1.SelectedIndex = 0;
                InnerModbusChannel tempChl = null;
                for (int i = 0; i < AppMain.ChlList.Count; i++)
                {
                    if (tvDevices.SelectedNode.Parent.Name == AppMain.ChlList[i].baseChlPara.ChannelId.ToString())
                    {
                        tempChl = AppMain.ChlList[i] as InnerModbusChannel;
                        if (tempChl == null)
                        {
                            return;
                        }
                        break;
                    }
                }
                if (tempChl == null)
                {
                    return;
                }
                for (int j =0;j < tempChl.m_DeviceList.Count;j++)
                {
                    if (tvDevices.SelectedNode.Name == tempChl.m_DeviceList[j].DeviceId.ToString())
                    {
                        SelNode = tvDevices.SelectedNode;
                        CurDev = tempChl.m_DeviceList[j];
                        InitDevInfo(tempChl.m_DeviceList[j]);
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void tvDevices_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                TreeNode node = tvDevices.GetNodeAt(e.X, e.Y);
                if (node != null)
                {
                    tvDevices.SelectedNode = node;
                    tvDevices_AfterSelect(sender, new TreeViewEventArgs(node));
                    if (CurDev != null)
                    {
                        cmtsmiAddChl.Enabled = false;
                        cmtsmiDelChl.Enabled = false;
                    }
                    else
                    {
                        cmtsmiAddChl.Enabled = true;
                        cmtsmiDelChl.Enabled = true;
                    }
                }
            }
        }
        private void btnAddData_Click(object sender, EventArgs e)
        {
            if (SelNode == null || CurDev == null)
            {
                return;
            }
             int rowIndex = 0;
            if (dgvDevInfo.SelectedCells.Count>0)
            {
                rowIndex = dgvDevInfo.SelectedCells[0].RowIndex;
                DataGridViewRow SelRow = dgvDevInfo.Rows[rowIndex];
                Int32 regAddr = 0,datalen =2,offset = 0;
                try
                {
                    regAddr = Convert.ToUInt16(SelRow.Cells[2].Value.ToString(), 16);
                    datalen = GetDataLen(SelRow.Cells[3].Value.ToString());
                    for (int i = rowIndex; i < rowIndex + (int)numAddData.Value; i++)
                    {
                        regAddr += datalen;
                        offset = i + 1;
                        DataGridViewRow row = (DataGridViewRow)SelRow.Clone();
                        row.SetValues(new object[]{SelRow.Cells[0].Value,SelRow.Cells[1].Value,regAddr.ToString("X4"),
                                           SelRow.Cells[3].Value,SelRow.Cells[4].Value,SelRow.Cells[5].Value,
                                           SelRow.Cells[6].Value,SelRow.Cells[7].Value});//,SelRow.Cells[8].Value
                        //dgvDevInfo.Rows.Insert(rowIndex, row);
                        dgvDevInfo.Rows.Insert(offset, row);
                    }
                    dgvDevInfo.ClearSelection();
                    dgvDevInfo.CurrentRow.Selected = false;
                    dgvDevInfo.Rows[offset].Selected = true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else
            {
                for (int i = 0; i < (int)numAddData.Value; i++)
                {
                    DataGridViewRow row = new DataGridViewRow();
                    row.CreateCells(dgvDevInfo);
                    row.SetValues(new object[] { "数据" + i.ToString(), "0x03", (i * 2).ToString("X4"), "INT32", "1.0", true, true, "5" });          
                    dgvDevInfo.Rows.Add(row);
                }
            }
        }

        private void btnDelData_Click(object sender, EventArgs e)
        {
            if (SelNode == null || CurDev == null)
            {
                MessageBox.Show(Resources.MsgSelDeviceTips, Resources.msgTitleTip);
                return;
            }
            if (dgvDevInfo.SelectedCells.Count <= 0)
            {
                return;
            }
            int SelCnt = dgvDevInfo.SelectedRows.Count;
            for (int i = SelCnt-1; i >=0 ;i-- )
            {
                dgvDevInfo.Rows.Remove(dgvDevInfo.SelectedRows[0]);
            }
            //int rowIndex = dgvDevInfo.SelectedCells[0].RowIndex;
            //if (rowIndex >= 0)
            //{
            //    dgvDevInfo.Rows.RemoveAt(rowIndex);
            //} 
        }

        private void dgvDevInfo_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            SolidBrush b = new SolidBrush(this.dgvDevInfo.RowHeadersDefaultCellStyle.ForeColor);
            e.Graphics.DrawString((e.RowIndex + 1).ToString(CultureInfo.CurrentUICulture), this.dgvDevInfo.DefaultCellStyle.Font,
                                   b, e.RowBounds.Location.X + 10, e.RowBounds.Location.Y + 4);
        }

        private void dgvChlPara_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            SolidBrush b = new SolidBrush(this.dgvChlPara.RowHeadersDefaultCellStyle.ForeColor);
            e.Graphics.DrawString((e.RowIndex + 1).ToString(CultureInfo.CurrentUICulture), this.dgvChlPara.DefaultCellStyle.Font,
                                   b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y + 4);
        }
        /// <summary>
        /// 点表导入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnImport_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofDlg = new OpenFileDialog();
            //ofDlg.InitialDirectory = AppMain.StartPath;
            ofDlg.Filter = Resources.txtItemTemplateFilterIni;
            ofDlg.Multiselect = false;
            if (ofDlg.ShowDialog() == DialogResult.OK)
            {
                INIFile iniTemplate = new INIFile(ofDlg.FileName);
                txtDevName.Text = iniTemplate.IniReadValue("公共信息", "装置名称");
                numAddr.Value = byte.Parse(iniTemplate.IniReadValue("公共信息", "装置地址"));
                ckbIsWorking.Checked = Boolean.Parse(iniTemplate.IniReadValue("公共信息", "是否启用"));
                cbbByteSqueue.SelectedIndex =(Int32) Enum.Parse(typeof(eByteSqueue),iniTemplate.IniReadValue("公共信息", "字节顺序"));
                numSendCnt.Value = Int32.Parse(iniTemplate.IniReadValue("公共信息", "断线重发"));
                if (!string.IsNullOrEmpty(iniTemplate.IniReadValue("公共信息", "断线触发")))
                {
                    numBreakCnt.Value = Int32.Parse(iniTemplate.IniReadValue("公共信息", "断线触发"));
                } 
                int Cnt = Int32.Parse(iniTemplate.IniReadValue("公共信息", "数据项个数"));

                dgvDevInfo.Rows.Clear();
                dgvDevInfo.Rows.Add(Cnt);
                try
                {
                    for (int i = 0; i < dgvDevInfo.RowCount; i++)
                    {
                        string strDataItem = "";
                        strDataItem = iniTemplate.IniReadValue("数据项信息", "数据项_" + i.ToString("D2"));
                        string[] strIni = strDataItem.Split(',');
                        dgvDevInfo.Rows[i].Cells[0].Value = strIni[0];   // 数据名称
                        dgvDevInfo.Rows[i].Cells[1].Value = strIni[1];   // 功能码    
                        dgvDevInfo.Rows[i].Cells[2].Value = strIni[2];   // 寄存器地址
                        dgvDevInfo.Rows[i].Cells[3].Value = strIni[3];   // 值类型
                        dgvDevInfo.Rows[i].Cells[4].Value = strIni[4];   // 缩放倍率
                        dgvDevInfo.Rows[i].Cells[5].Value = Boolean.Parse(strIni[5]);   // 是否锁定 
                        dgvDevInfo.Rows[i].Cells[6].Value = (strIni[6] != "True" && strIni[6] != "False") || Boolean.Parse(strIni[6]);// 是否存储
                        dgvDevInfo.Rows[i].Cells[7].Value = strIni[7];// 存储周期
                        //dgvDevInfo.Rows[i].Cells[8].Value = strIni[8];// 修改值最小间隔
                    }
                    if (ccbScript.Checked)
                    {
                        LoadScriptXml(ofDlg.FileName.Replace(".ini", ".scr"));
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }
                MessageBox.Show(Resources.MsgImportSucceed, Resources.msgTitleTip);
            }           
        }
        /// <summary>
        /// 点表导出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnExport_Click(object sender, EventArgs e)
        {
            //选择创建文件的路径
            SaveFileDialog save = new SaveFileDialog();
            save.DefaultExt = "ini";
            save.AddExtension = true;
            save.Filter = Resources.txtItemTemplateFilterIni;
            save.Title = Resources.txtPleaseSelSavePath;
            save.FileName = txtDevName.Text;
            //save.InitialDirectory = AppMain.StartPath;
            if (save.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            INIFile iniTemplate = new INIFile(save.FileName);
            iniTemplate.ClearAllSection();
            iniTemplate.IniWriteValue("公共信息", "装置名称", txtDevName.Text);
            iniTemplate.IniWriteValue("公共信息", "装置地址", numAddr.Value.ToString());
            iniTemplate.IniWriteValue("公共信息", "是否启用", ckbIsWorking.Checked.ToString());
            iniTemplate.IniWriteValue("公共信息", "字节顺序", Enum.GetName(typeof(eByteSqueue),cbbByteSqueue.SelectedIndex));
            iniTemplate.IniWriteValue("公共信息", "断线重发", numSendCnt.Value.ToString());
            iniTemplate.IniWriteValue("公共信息", "断线触发", numBreakCnt.Value.ToString());
            iniTemplate.IniWriteValue("公共信息", "数据项个数", dgvDevInfo.RowCount.ToString());
            StringBuilder strB = new StringBuilder();
            for (int i =0; i < dgvDevInfo.RowCount;i++)
            {
                strB.Clear();
                strB.Append(dgvDevInfo.Rows[i].Cells[0].Value.ToString());      // 数据名称
                strB.Append(","+dgvDevInfo.Rows[i].Cells[1].Value.ToString());  // 功能码    
                strB.Append(","+dgvDevInfo.Rows[i].Cells[2].Value.ToString());  // 寄存器地址
                strB.Append(","+dgvDevInfo.Rows[i].Cells[3].Value.ToString());  // 值类型
                strB.Append(","+dgvDevInfo.Rows[i].Cells[4].Value.ToString());  // 缩放倍率
                strB.Append(","+dgvDevInfo.Rows[i].Cells[5].Value.ToString());  // 是否锁定
                strB.Append("," + dgvDevInfo.Rows[i].Cells[6].Value.ToString());  // 是否存储
                strB.Append("," + dgvDevInfo.Rows[i].Cells[7].Value.ToString());  // 存储周期
                //strB.Append("," + dgvDevInfo.Rows[i].Cells[8].Value.ToString());  // 
                iniTemplate.IniWriteValue("数据项信息", "数据项_" + i.ToString("D2"), strB.ToString());
            }
            if (ccbScript.Checked)
            {
                SaveScriptXml(save.FileName.Replace(".ini", ".scr"));
            }
            MessageBox.Show(Resources.MsgExportSucceed, Resources.msgTitleTip);
        }

        /// <summary>
        /// 导入脚本
        /// </summary>
        /// <returns></returns>
        public bool LoadScriptXml(string strPath)
        {
            XmlDocument doc = new XmlDocument();
            try
            {
                doc.Load(strPath);
                XmlNode xnparent = doc.SelectSingleNode("ScriptDoc");
                XmlNode devXn = xnparent.SelectSingleNode("DevScript");
                CurDev.Expression = devXn.InnerText;

                XmlNode dataItemXn = xnparent.SelectSingleNode("DevItemScript");
                for (int i = 0; i < CurDev.GetDataItemCnt(); i++)
                {
                    XmlNode xn = dataItemXn.SelectSingleNode(CurDev.GetDataItem(i).Name);
                    CurDev.GetDataItem(i).Expression = xn.InnerText;
                    dgvDevInfo.Rows[i].Tag = xn.InnerText;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(Resources.MsgFailedToImportScript + ex.Message, Resources.msgTitleTip);
                return false;
            }
            return true;
        }
        /// <summary>
        /// 导出脚本
        /// </summary>
        /// <returns></returns>
        public bool SaveScriptXml(string strPath)
        {
            XmlDocument doc1 = new XmlDocument();
            try
            {
                if (File.Exists(strPath))
                {
                    File.Delete(strPath);
                }
                //doc1.Load(strPath);
                XmlNode newNodeParent = doc1.CreateElement("ScriptDoc");

                XmlNode devXn =  doc1.CreateElement("DevScript");
                devXn.InnerText = CurDev.Expression;
                newNodeParent.AppendChild(devXn);
                
                XmlNode dataItemXn = doc1.CreateElement("DevItemScript");
                for (int i = 0; i < CurDev.GetDataItemCnt(); i++)
                {
                    XmlNode xn = doc1.CreateElement(CurDev.GetDataItem(i).Name);
                    xn.InnerText = CurDev.GetDataItem(i).Expression;
                    dataItemXn.AppendChild(xn);
                }
                newNodeParent.AppendChild(dataItemXn);
                doc1.AppendChild(newNodeParent);
                doc1.Save(strPath);
            }
            catch (Exception ex)
            {
                MessageBox.Show(Resources.MsgFailedToExportScript + ex.Message, Resources.msgTitleTip);
                return false;
            }
            return true;
        }


        /// <summary>
        /// 应用当前通道参数 (参数保存至缓存)
        /// </summary>
        /// <returns></returns>
        private bool ApplyChlPara()
        {
            try
            {
                InnerModbusChannel tempChl = null;
                int ChlCnt = -1;
                for (int i = 0; i < AppMain.ChlList.Count;i++ )
                {
                    tempChl = AppMain.ChlList[i] as InnerModbusChannel;
                    if (tempChl == null)
                    {
                        continue;
                    }
                    ChlCnt++;
                    tempChl.ComChlPara.ChannelName = dgvChlPara.Rows[ChlCnt].Cells[0].Value.ToString();
                    tempChl.ComChlPara.ComPort.PortName = dgvChlPara.Rows[ChlCnt].Cells[1].Value.ToString();
                    tempChl.ComChlPara.ComPort.BaudRate = Int32.Parse(dgvChlPara.Rows[ChlCnt].Cells[2].Value.ToString());
                    tempChl.PollingTime = Int32.Parse(dgvChlPara.Rows[ChlCnt].Cells[3].Value.ToString());
                    tempChl.ComChlPara.iTimeOut = Int32.Parse(dgvChlPara.Rows[ChlCnt].Cells[4].Value.ToString());
                    tempChl.ComChlPara.ComPort.ReadTimeout = tempChl.ComChlPara.iTimeOut;
                    tempChl.ComChlPara.ComPort.Parity = (Parity)Enum.Parse(typeof(Parity), dgvChlPara.Rows[ChlCnt].Cells[5].Value.ToString());
                    tempChl.ComChlPara.ComPort.DataBits = Int32.Parse(dgvChlPara.Rows[ChlCnt].Cells[6].Value.ToString());
                    switch(dgvChlPara.Rows[ChlCnt].Cells[7].Value.ToString())
                    {
                        case "1":
                            tempChl.ComChlPara.ComPort.StopBits = StopBits.One;
                            break;
                        case "1.5":
                            tempChl.ComChlPara.ComPort.StopBits = StopBits.OnePointFive;
                            break;
                        case "2":
                            tempChl.ComChlPara.ComPort.StopBits = StopBits.Two;
                            break;
                        default:
                            tempChl.ComChlPara.ComPort.StopBits = StopBits.One;
                            break;
                    }                    
                    if (tempChl.ComChlPara.ComPort.ReadTimeout<0)
                    {
                        tempChl.ComChlPara.ComPort.ReadTimeout = 25;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return false;
            }
            return true;
        }
        /// <summary>
        /// 保存当前显示参数 只保存至缓存
        /// </summary>
        /// <returns></returns>
        private bool ApplyCurDevInfo()
        {
            if (CurDev == null)
            {
                return false;
            }
            try
            {
                CurDev.Name = txtDevName.Text;
                CurDev.bAddr = (byte)numAddr.Value;
                CurDev.byteSqueue = (eByteSqueue)Enum.ToObject(typeof(eByteSqueue), cbbByteSqueue.SelectedIndex);
                CurDev.isWorking = (ckbIsWorking.CheckState== CheckState.Checked ? true: false);
                CurDev.MaxSendCnt = (int)(numSendCnt.Value + 1);
                CurDev.BreakOffCnt = (int)numBreakCnt.Value;
                DevDataItem DataItem;
                CurDev.GetDataList().Clear();
                for (int i = 0; i < dgvDevInfo.Rows.Count; i++)
                {
                    DataItem = new DevDataItem();
                    DataItem.Name = dgvDevInfo[0, i].Value.ToString();
                    DataItem.bFunCode = Convert.ToByte(dgvDevInfo[1, i].Value.ToString().Substring(2),16);     // "功能码"
                    DataItem.usRegAddr = Convert.ToUInt16(dgvDevInfo[2, i].Value.ToString(), 16);               //  "地址"
                    DataItem.dataType = (eDataType)Enum.Parse(typeof(eDataType), dgvDevInfo[3, i].Value.ToString()); ;  // "值类型"
                    DataItem.dmult = float.Parse(dgvDevInfo[4, i].Value.ToString());                                    // "倍率" 
                    DataItem.IsSummation = (bool)((DataGridViewCheckBoxCell)dgvDevInfo[5, i]).Value;                         // 是否累加值
                    DataItem.IsSave = (bool)((DataGridViewCheckBoxCell)dgvDevInfo[6, i]).Value;  
                    DataItem.SaveCycle = Int32.Parse(dgvDevInfo[7, i].Value.ToString());
                    DataItem.Expression = dgvDevInfo.Rows[i].Tag as string;
                    CurDev.GetDataList().Add(DataItem);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return false;
            }
            RefreshChlDevTree();
            return true;
        }
        /// <summary>
        /// 保存所有参数至数据库
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmiSaveCur_Click(object sender, EventArgs e)
        {
            ApplyChlPara();
            ApplyCurDevInfo();
            if (AppMain.SaveChlParaToDB() == true)
            {
                MessageBox.Show(Resources.MsgSaveSucceed);
            }
            else
            {
                MessageBox.Show(Resources.MsgSaveFailed);
            }
            //if (MessageBox.Show("是否删除历史数据？") == DialogResult.Yes)
            //{
            //}
            // 默认删除历史库
            using (OleDbConnection con = new OleDbConnection(dbOper.M_str_sqlcon))
            {
                try
                {
                    OleDbCommand cmd = new OleDbCommand();
                    cmd.Connection = con;
                    con.Open();
                    cmd.CommandText = "select name from MSysObjects where type=1 and flags=0"; // 如果出现"没有读取MSysObjects读取权限" 需设置Access的显示隐藏对象和系统对象，更改MSysObjects的权限属性
                    OleDbDataReader reader = cmd.ExecuteReader();
                    List<string> sqlList = new List<string>();
                    while (reader.Read())
                    {
                        string strName = reader.GetString(0);
                        if (strName.Contains(Resources.HistoryTableName + "2"))
                        {
                            sqlList.Add(reader.GetString(0));
                        }
                    }
                    reader.Close();
                    for (int i = 0; i < sqlList.Count; i++)//truncate(Access 不支持) 删除内容、释放空间但不删除定义。与drop不同的是,他只是清空表数据而已,他比较温柔. 
                    {
                        cmd.CommandText = string.Format("drop table {0}", sqlList[i]);
                        cmd.ExecuteNonQuery();
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }          
        }

        /// <summary>
        /// 应用当前参数（不保存）
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmiApplyAllPara_Click(object sender, EventArgs e)
        {
            bool res1, res2;
            res1 = ApplyChlPara();
            res2 = ApplyCurDevInfo();
            if (res1)
            {
                MessageBox.Show(Resources.MsgSucceedToConfig);
            }
            else
            {
                MessageBox.Show(Resources.MsgFailedToConfig);
            }
           
        }
        private void tsmiReload_Click(object sender, EventArgs e)
        {
            if (AppMain.LoadChlDevPara()== false)
            {
                MessageBox.Show(Resources.MsgFailedToLoadConfig);
            }
            else
            {
                MessageBox.Show(Resources.MsgSucceedToLoadConfig);
            }
            tvDevices.SelectedNode = null;
            CurDev = null;
            RefreshChlDevTree();
            if (AppMain.ChlList.Count > 0)
            {
                try
                {
                    tvDevices.SelectedNode = tvDevices.Nodes[0].Nodes[0].Nodes[0];
                }
                catch
                {
                    tvDevices.SelectedNode = null;
                }
            }
            
        }

        private void tsmiAddChannel_Click(object sender, EventArgs e)
        {
            tabControl1.SelectedIndex = 1; //切换tabControl显示
            InnerModbusChannel InnerChl = new InnerModbusChannel(new ComPara(AppMain.ChlList.Count,Resources.txtNewChannel));
            AppMain.ChlList.Add(InnerChl);
            RefreshChlDevTree();
        }

        private void tsmiDelChnnel_Click(object sender, EventArgs e)
        {
            tabControl1.SelectedIndex = 1;//切换tabControl显示
            //tabControl1.TabPages[0].Parent = null; // 隐藏TabPage
            if (tvDevices.SelectedNode.Name == strRootName)
            {
                if (DialogResult.Yes == MessageBox.Show(Resources.MsgDelAllChannelOrNot,Resources.msgTitleTip, MessageBoxButtons.YesNo, MessageBoxIcon.Warning))
                {
                    for (int i = AppMain.ChlList.Count-1; i >= 0 ;i-- )
                    {
                        if (AppMain.ChlList[i] is InnerModbusChannel)
                        {
                            AppMain.ChlList.RemoveAt(i);
                        }
                    }
                }
            }
            else if (tvDevices.SelectedNode.Parent.Name == strRootName)
            {
                int ChlId = Int32.Parse(tvDevices.SelectedNode.Name);
                for (int i = 0; i < AppMain.ChlList.Count; i++)
                {
                    if (AppMain.ChlList[i].baseChlPara.ChannelId == ChlId)
                    {
                        AppMain.ChlList.RemoveAt(i);
                    }
                }
            }
            AppMain.RefreshChlId();
            RefreshChlDevTree();
        }

        private void tsmiAddDevice_Click(object sender, EventArgs e)
        {
            InnerModbusChannel tempChl = null;
            if (SelNode == null && tvDevices.SelectedNode.Parent.Name == strRootName)
            {
                for (int i = 0; i < AppMain.ChlList.Count; i++)
                {
                    if (tvDevices.SelectedNode.Name == AppMain.ChlList[i].baseChlPara.ChannelId.ToString())
                    {
                        tempChl = AppMain.ChlList[i] as InnerModbusChannel;
                        if (tempChl == null)
                        {
                            return;
                        }
                        break;
                    }
                }
            }
            else
            {
                for (int i = 0; i < AppMain.ChlList.Count; i++)
                {
                    if (tvDevices.SelectedNode.Parent.Name == AppMain.ChlList[i].baseChlPara.ChannelId.ToString())
                    {
                        tempChl = AppMain.ChlList[i] as InnerModbusChannel;
                        if (tempChl == null)
                        {
                            return;
                        }
                        break;
                    }
                }
            }
            if (tempChl != null)
            {
                tempChl.m_DeviceList.Add(new InnerModbusDevice(Resources.txtNewMeter, tempChl));
                RefreshChlDevTree();
            }
        }
        /// <summary>
        /// 删除设备
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmiDelDevice_Click(object sender, EventArgs e)
        {
            InnerModbusChannel tempChl = null;
            if (SelNode != null && CurDev != null)
            {
                for (int i = 0; i < AppMain.ChlList.Count; i++)
                {
                    if (tvDevices.SelectedNode.Parent.Name == AppMain.ChlList[i].baseChlPara.ChannelId.ToString())
                    {
                        tempChl = AppMain.ChlList[i] as InnerModbusChannel;
                        if (tempChl == null)
                        {
                            return;
                        }
                        break;
                    }
                }
            }
            if (tempChl != null)
            {
                tempChl.m_DeviceList.Remove(CurDev);
                SelNode = null; 
                CurDev = null;
                Channel chl = tempChl;
                AppMain.ReFreshDevId(ref chl);
                RefreshChlDevTree();
            }
        }

        private void dgvChlPara_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right && e.RowIndex > -1 && e.ColumnIndex > -1)
            {
                dgvChlPara.ClearSelection();
                if (dgvChlPara.CurrentRow != null) dgvChlPara.CurrentRow.Selected = false;
                dgvChlPara.Rows[e.RowIndex].Selected = true;
            }
        }

        private void dgvDevInfo_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right && e.RowIndex > -1 && e.ColumnIndex > -1)
            {
                dgvDevInfo.ClearSelection();
                if (dgvDevInfo.CurrentRow != null) dgvDevInfo.CurrentRow.Selected = false;
                dgvDevInfo.Rows[e.RowIndex].Selected = true;
            }
        }
        /// <summary>
        /// 增加通道
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            InnerModbusChannel InnerChl = new InnerModbusChannel(new ComPara(AppMain.ChlList.Count, Resources.txtNewChannel));
            AppMain.ChlList.Add(InnerChl);
            RefreshChlDevTree();
        }
        /// <summary>
        /// 删除通道
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            if (dgvChlPara.SelectedRows == null || dgvChlPara.SelectedRows.Count == 0)
            {
                return;
            }
            int ChlIndex = -1;
            int RowIndex = dgvChlPara.SelectedRows[0].Index;
            for (int i = 0; i < AppMain.ChlList.Count; i++)
            {
                if (AppMain.ChlList[i] is InnerModbusChannel)
                {
                    ChlIndex++;
                }
                if (ChlIndex == RowIndex)
                {
                    AppMain.ChlList.RemoveAt(i);
                    AppMain.RefreshChlId();
                    RefreshChlDevTree();
                    break;
                }
            }
        }

        /// <summary>
        /// 添加数据项
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripMenuItem3_Click(object sender, EventArgs e)
        {
            if (SelNode == null || CurDev == null)
            {
                return;
            }
            int rowIndex = 0;
            if (dgvDevInfo.SelectedCells.Count > 0)
            {
                rowIndex = dgvDevInfo.SelectedCells[0].RowIndex;
                DataGridViewRow SelRow = dgvDevInfo.Rows[rowIndex];
                Int32 regAddr = 0, datalen = 2;

                try
                {
                    regAddr = Convert.ToUInt16(SelRow.Cells[2].Value.ToString(), 16); 
                                        datalen = GetDataLen(SelRow.Cells[3].Value.ToString());

                    regAddr += datalen;
                    DataGridViewRow row = (DataGridViewRow)SelRow.Clone();
                    row.SetValues(new object[]{SelRow.Cells[0].Value,SelRow.Cells[1].Value,regAddr.ToString("X4"),
                                        SelRow.Cells[3].Value,SelRow.Cells[4].Value,SelRow.Cells[5].Value,
                                        SelRow.Cells[6].Value,SelRow.Cells[7].Value});//,SelRow.Cells[8].Value
                    //dgvDevInfo.Rows.Insert(rowIndex, row);
                    dgvDevInfo.Rows.Insert(++rowIndex, row);
                    dgvDevInfo.ClearSelection();
                    dgvDevInfo.CurrentRow.Selected = false;
                    dgvDevInfo.Rows[rowIndex].Selected = true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else
            {
                DataGridViewRow row = new DataGridViewRow();
                row.CreateCells(dgvDevInfo);
                row.SetValues(new object[] { Resources.txtNewDataItem, "0x03", 0.ToString("X4"), "INT32", "1.0", true, true, "5" });
                dgvDevInfo.Rows.Add(row);
            }
        }
        /// <summary>
        /// 删除数据项
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripMenuItem4_Click(object sender, EventArgs e)
        {
            if (SelNode == null || CurDev == null)
            {
                return;
            }
            if (dgvDevInfo.SelectedCells.Count <= 0)
            {
                return;
            }
            int rowIndex = dgvDevInfo.SelectedCells[0].RowIndex;
            if (rowIndex >= 0)
            {
                dgvDevInfo.Rows.RemoveAt(rowIndex);
            }
        }
        /// <summary>
        /// 应用修改值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnApplyCurDevPara_Click(object sender, EventArgs e)
        {
            if (ApplyCurDevInfo() == true)
            {
                MessageBox.Show(Resources.MsgSucceedToConfig, Resources.msgTitleTip);
            }
            else
            {
                MessageBox.Show(Resources.MsgFailedToConfig, Resources.msgTitleTip);
            }
        }

        private void dgvDevInfo_ColumnHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (SelNode == null || CurDev == null)
            {
                return;
            }

            ValueInput frm = new ValueInput();
            for (int i = 0; i < dgvDevInfo.Rows.Count; i++)
            {
                if (dgvDevInfo.Rows[i].Cells[e.ColumnIndex].Value != null)
                {
                    frm.strList.Add(dgvDevInfo.Rows[i].Cells[e.ColumnIndex].Value.ToString());
                }
            }

            if (e.ColumnIndex == 0) // 数据项名称
            {
                if (frm.ShowDialog() == DialogResult.OK)
                {
                    for (int i = 0; i < frm.strList.Count; i++)
                    {
                        if (i < CurDev.GetDataItemCnt())
                        {
                             CurDev.GetDataItem(i).Name = frm.strList[i];
                        }
                        if (i < dgvDevInfo.Rows.Count)
                       {
                           dgvDevInfo.Rows[i].Cells[e.ColumnIndex].Value = frm.strList[i];
                       }
                    }
                }
            }
            else if (e.ColumnIndex == 2) // 寄存器地址
            {
                if (frm.ShowDialog() == DialogResult.OK)
                {
                    ushort tempVal = 0;
                    for (int i = 0; i < frm.strList.Count; i++)
                    {
                        try
                        {
                            tempVal = Convert.ToUInt16(frm.strList[i], 16);
                        }
                        catch
                        {
                            continue;
                        }
                        if (i < CurDev.GetDataItemCnt())
                        {
                            CurDev.GetDataItem(i).usRegAddr = tempVal;
                        }
                        if (i < dgvDevInfo.Rows.Count)
                        {
                            dgvDevInfo.Rows[i].Cells[e.ColumnIndex].Value = frm.strList[i];
                        }
                    }
                }
            }
            else if (e.ColumnIndex == 3) // 值类型
            {
                if (frm.ShowDialog() == DialogResult.OK)
                {
                    eDataType tempVal = 0;
                    for (int i = 0; i < frm.strList.Count; i++)
                    {
                        try
                        {
                            tempVal = (eDataType)Enum.Parse(typeof(eDataType), frm.strList[i]);
                        }
                        catch
                        {
                            continue;
                        }
                        if (i < CurDev.GetDataItemCnt())
                        {
                            CurDev.GetDataItem(i).dataType = tempVal;
                        }
                        if (i < dgvDevInfo.Rows.Count)
                        {
                            dgvDevInfo.Rows[i].Cells[e.ColumnIndex].Value = frm.strList[i];
                        }
                    }
                }
            }
            else if (e.ColumnIndex == 4) // 缩放倍率
            {
                if (frm.ShowDialog() == DialogResult.OK)
                {
                    float tempVal = 0;
                    for (int i = 0; i < frm.strList.Count; i++)
                    {
                        try
                        {
                            tempVal = Convert.ToSingle(frm.strList[i]);
                        }
                        catch
                        {
                            continue;
                        }                                           
                        if (i < CurDev.GetDataItemCnt())
                        {
                            CurDev.GetDataItem(i).dmult = tempVal;
                        }
                        if (i < dgvDevInfo.Rows.Count)
                        {
                            dgvDevInfo.Rows[i].Cells[e.ColumnIndex].Value = frm.strList[i];
                        }
                    }
                }
            }
            else if (e.ColumnIndex == 5) // 允许读取
            {
                if (CurDev.GetDataItemCnt() <= 0)
                {
                    return;
                }
                bool bValue = CurDev.GetDataItem(0).IsSummation;
                for (int i = 0; i < dgvDevInfo.RowCount; i++)
                {
                    dgvDevInfo.Rows[i].Cells[e.ColumnIndex].Value = !bValue;
                    if (i < CurDev.GetDataItemCnt())
                    {
                        CurDev.GetDataItem(i).IsSummation = !bValue;
                    }
                }
            }
            else if (e.ColumnIndex == 6) // 是否存储
            {
                if (CurDev.GetDataItemCnt() <= 0)
                {
                    return;
                }
                bool bValue = CurDev.GetDataItem(0).IsSave;
                for (int i = 0; i < dgvDevInfo.RowCount; i++)
                {
                    dgvDevInfo.Rows[i].Cells[e.ColumnIndex].Value = !bValue;
                    if (i < CurDev.GetDataItemCnt())
                    {
                        CurDev.GetDataItem(i).IsSave = !bValue;
                    }
                }
                //if (frm.ShowDialog() == DialogResult.OK)
                //{
                //    bool tempVal = true;
                //    for (int i = 0; i < frm.strList.Count && i < CurDev.GetDataItemCnt() && i < dgvDevInfo.Rows.Count; i++)
                //    {
                //        try
                //        {
                //            tempVal = Convert.ToBoolean(frm.strList[i]);
                //        }
                //        catch
                //        {
                //            continue;
                //        }
                //        CurDev.GetDataItem(i).IsSave = tempVal;
                //        dgvDevInfo.Rows[i].Cells[e.ColumnIndex].Value = tempVal;
                //    }
                //}
            }
            else if (e.ColumnIndex == 7) // 存储周期
            {
                if (frm.ShowDialog() == DialogResult.OK)
                {
                    Int32 tempVal = 0;
                    for (int i = 0; i < frm.strList.Count && i < CurDev.GetDataItemCnt() && i < dgvDevInfo.Rows.Count; i++)
                    {
                        try
                        {
                            tempVal = Convert.ToInt32(frm.strList[i]);
                        }
                        catch
                        {
                            continue;
                        }
                        if (i < CurDev.GetDataItemCnt())
                        {
                            CurDev.GetDataItem(i).SaveCycle = tempVal;
                        }
                        if (i < dgvDevInfo.Rows.Count)
                        {
                            dgvDevInfo.Rows[i].Cells[e.ColumnIndex].Value = CurDev.GetDataItem(i).SaveCycle;
                        }
                    }
                }
            }
            Console.WriteLine("当前列号为: "+e.ColumnIndex);
        }

        private void dgvDevInfo_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            e.Cancel = true;
        }

        private void dgvDevInfo_CellClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void btnScriptEdit_Click(object sender, EventArgs e)
        {
            ApplyCurDevInfo();
            ScriptEdit editForm = new ScriptEdit(CurDev);
            editForm.ShowDialog();
            UpdateScript();
        }
        /// <summary>
        /// 更新当前设备的脚本 保存至dgvDevInfo.Rows[i].Tag
        /// </summary>
        /// <returns></returns>
        private bool UpdateScript()
        {
            if (CurDev == null)
            {
                return false;
            }
            try
            {
                DevDataItem DataItem;
                for (int i = 0; i < dgvDevInfo.Rows.Count; i++)
                {
                    DataItem = CurDev.GetDataItem(i);//new DevDataItem();
                    dgvDevInfo.Rows[i].Tag = DataItem.Expression;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return false;
            }
            return true;
        }

        /// <summary>
        /// 根据数据类型获取数据长度
        /// </summary>
        /// <param name="strDataType"></param>
        /// <returns></returns>
        private int GetDataLen(string strDataType)
        {
            eDataType DataType;
            try
            {
               DataType = (eDataType)Enum.Parse(typeof(eDataType), strDataType);
            }
            catch 
            {
            	return 2;
            }
             

            int stepCnt = 2;
            if (DataType == eDataType.INT32 || DataType == eDataType.UINT32 || DataType == eDataType.FLOAT || DataType == eDataType.BCD8 || DataType == eDataType.BCD6_2 || DataType == eDataType.BCD4_4)
            {
                stepCnt = 2; // 两个寄存器 
            }
            else if (DataType == eDataType.SHORT || DataType == eDataType.USHORT || DataType == eDataType.BCD4 || DataType == eDataType.BCD2_2 || DataType == eDataType.BCD1_3 || DataType == eDataType.WORD || DataType == eDataType.BYTE)
            {
                stepCnt = 1; // 一个寄存器
            }
            else if (DataType == eDataType.DOUBLE || DataType == eDataType.LONG)
            {
                stepCnt = 4;
            }
            return stepCnt;
        }

        //private void ModbusDevPara_Activated(object sender, EventArgs e)
        //{
        //    //AppMain.StopAllChl();
        //    //AppMain.MainForm.FrmShowValue.timer1.Enabled = false;   // 停止数据刷新
        //}

        //private void ModbusDevPara_Deactivate(object sender, EventArgs e)
        //{
        //    //AppMain.MainForm.FrmShowValue.RefreshChlTree();
        //    //AppMain.MainForm.FrmShowMsg.RefreshChlTree();
        //    //AppMain.MainForm.FrmShowValue.timer1.Enabled = true;   // 开始数据刷新
        //    ApplyChlPara();
        //    ApplyCurDevInfo();
        //}

    }
}
